import { Search } from 'vant'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/'



Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location, onResolve, onReject) {

if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)

return originalPush.call(this, location).catch(err => err)

}


const routes = [
  {
    path: '/',
    redirect:'home',
  
  },
  {
    path: '/tipic',
    name:'Tipic',
    meta:{
      showBar:true
    },
    component:()=> import('@/views/Tipic/')
  },
  {
    path: '/category',
    name:'Category',
    meta:{
      showBar:true,
      navigiton:true,
      alias:"分类产品"
    },
    component:()=> import('@/views/Category/')
  },
  {
    path: '/cart',
    name:'Cart',
    meta:{
      // showBar:true,
      navigiton:true,
      alias:"购物车"
    },
    component:()=> import('@/views/Cart/')
  },
  {
    path: '/user',
    name:'User',
    meta:{
      showBar:true
    },
    component:()=> import('@/views/User/')
  },
  {
    path: '/register',
    name:'Register',
    component:()=> import('@/views/Register/')
  },
  {
    path: '/detail',
    name:'Detail',
    meta:{
      navigiton:true,
      alias:"商品详情"
    },
    component:()=> import('@/views/Detail/')
  },
  {
    path: '/brand',
    name:'Brand',
    meta:{
      // showBar:true,
      navigiton:true,
      alias:"品牌详情"
    },
    component:()=> import('@/views/Brand/')
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      showBar:true
    },
    children:[{
      path:'search',
      name:Search,
      component:()=> import('@/views/home/search/')
    }]
  },
]

const router = new VueRouter({
  base:'/lz/',
  routes
})

router.beforeEach((to,from,next)=>{

  if(to.path == '/cart'){
    let token = sessionStorage.getItem('token') || ''
    
    if(!token){
      Vue.prototype.$toast.fail('您没有登录')
      setTimeout(()=>{
        return next({path:'user'})
      },1000)
    }
  }
  next()
})


export default router
